/**
 * FileContentManager is a Java based file manager desktop application, it can
 * show, edit and manipulate the content of the files archived inside a zip.
 * 
 * Copyright (C) 2007
 * 
 * Created by Camila Sanchez [http://mimix.wordpress.com/], Rafael Naufal
 * [http://rnaufal.livejournal.com] and Rodrigo [rdomartins@gmail.com]
 * 
 * FileContentManager is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with FileContentManager; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package br.com.jteam.jfcm.gui.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;

import br.com.jteam.jfcm.controller.Subscriber;
import br.com.jteam.jfcm.controller.event.Event;
import br.com.jteam.jfcm.controller.event.OpenFileEvent;
import br.com.jteam.jfcm.controller.event.OpenHistoryFileEvent;
import br.com.jteam.jfcm.gui.PresentationFile;
import br.com.jteam.jfcm.gui.tree.ZipData;
import br.com.jteam.jfcm.gui.tree.ZipDataImpl;
import br.com.jteam.jfcm.gui.tree.ZipTree;

/**
 * Pain&eacute;l que cont&eacute;m uma &aacute;rvore uma lista de arquivos e
 * &eacute; notificado da ocorr&ecirc;ncia do {@link OpenFileEvent} e
 * {@link OpenHistoryFileEvent}.
 * 
 * @see br.com.jteam.jfcm.controller.event.OpenFileEvent
 * @see br.com.jteam.jfcm.controller.event.OpenHistoryFileEvent
 */
public class TreePanel extends JPanel implements Subscriber
{
	{
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		setBackground(Color.WHITE);
	}

	/**
	 * Adiciona o arquivo aberto em um scrollpane.
	 * 
	 * @param file
	 *            Arquivo aberto.
	 * 
	 */
	private void createScrollPane(PresentationFile<?> file)
	{
		removeAll();
		ZipTree ziptree = new ZipTree(createZipData(file));
		JScrollPane scrollPane = new JScrollPane(ziptree);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		add(scrollPane, BorderLayout.CENTER);
		SwingUtilities.updateComponentTreeUI(this);
	}

	/**
	 * @param rootFile
	 *            Arquivo aberto.
	 * @return Um ZipData contendo os dados dos n&oacute;s da &aacute;rvore.
	 */
	private ZipData createZipData(PresentationFile<?> rootFile)
	{
		Iterator<PresentationFile<?>> childrenFiles = rootFile.iterator();
		if (!childrenFiles.hasNext())
		{
			return new ZipDataImpl(rootFile);
		}
		List<ZipData> list = new LinkedList<ZipData>();
		for (Object file : rootFile)
		{
			ZipData data = createZipData((PresentationFile<?>) file);
			list.add(data);
		}
		return new ZipDataImpl(	rootFile,
								list.toArray(new ZipDataImpl[list.size()]));
	}

	public void notify(Event event)
	{
		if (event.getClass().equals(OpenHistoryFileEvent.class))
		{
			OpenHistoryFileEvent historyEvent = (OpenHistoryFileEvent) event;
			createScrollPane(historyEvent.getPresentationFile());
		} else if (event.getClass().equals(OpenFileEvent.class))
		{
			OpenFileEvent openEvent = (OpenFileEvent) event;
			createScrollPane(openEvent.getPresentationFile());
		}
	}
}
